package com.changgou.user.service.impl;

import com.changgou.user.dao.FootprintMapper;
import com.changgou.user.pojo.Collect;
import com.changgou.user.pojo.Footprint;
import com.changgou.user.service.FootprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class FootprintServiceImpl implements FootprintService {

    @Autowired
    private FootprintMapper footprintMapper;

    @Override
    public void save(String skuId, String username) {

        Footprint footprint = new Footprint();
        footprint.setSkuId(skuId);
        footprint.setUsername(username);

        List<Footprint> select = footprintMapper.select(footprint);

        if (select.size() >= 1) {
            return;
        }

        footprint.setDate(new Date());

        Footprint footprint1 = new Footprint();
        footprint1.setUsername(username);

        int count = footprintMapper.selectCount(footprint1);

        if (count >= 20) {
            Date date = footprintMapper.minDate();
            Footprint footprint2 = new Footprint();
            footprint2.setDate(date);
            footprintMapper.delete(footprint2);
        }

        footprintMapper.insertSelective(footprint);

    }

    @Override
    public List<Footprint> findBySkuId(String username) {
        Example example = new Example(Footprint.class);
        example.createCriteria().andEqualTo("username",username);
        example.orderBy("date").desc();
        List<Footprint> select = footprintMapper.selectByExample(example);
        return select;
    }
}
